package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    private static final Integer QI_SHOU = 1;
    private static final Integer TING_SHOU = 0;
    public static final Integer TO_BE_CONFIRMED = 2;
    public static final Integer CONFIRMED = 3;
    public static final Integer COMPLETED = 5;
    public static final Integer CANCELLED = 6;

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO DishOverViewVO() {
        Integer sold = dishMapper.DishOverViewVO(QI_SHOU);
        Integer discontinued = dishMapper.DishOverViewVO(TING_SHOU);
        return DishOverViewVO.builder().discontinued(discontinued).sold(sold).build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer sold = setmealMapper.overviewSetmeals(QI_SHOU);
        Integer discontinued = setmealMapper.overviewSetmeals(TING_SHOU);
        return SetmealOverViewVO.builder().discontinued(discontinued).sold(sold).build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders(LocalDateTime beginTime,LocalDateTime endTime) {
        Integer waitingOrders = orderMapper.overviewOrders(beginTime, endTime, TO_BE_CONFIRMED);
        Integer deliveredOrders = orderMapper.overviewOrders(beginTime, endTime, CONFIRMED);
        Integer completedOrders = orderMapper.overviewOrders(beginTime, endTime, COMPLETED);
        Integer cancelledOrders = orderMapper.overviewOrders(beginTime, endTime, CANCELLED);
        Integer allOrders = orderMapper.overviewOrders(beginTime, endTime, null);
        return new OrderOverViewVO(waitingOrders, deliveredOrders, completedOrders, cancelledOrders, allOrders);
    }

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime,LocalDateTime endTime) {
        Map map1 = new HashMap();
        map1.put("beginTime", beginTime);
        map1.put("endTime", endTime);
        map1.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.turnoverStatistics(map1);
        turnover=turnover==null?0.0:turnover;
        Map map2 = new HashMap();
        map2.put("beginTime", beginTime);
        map2.put("endTime", endTime);
        Integer newUsers = userMapper.userStatistics(map2);
        Integer validOrderCount = orderMapper.ordersStatistics(map1);
        Map map3 = new HashMap();
        map3.put("beginTime", beginTime);
        map3.put("endTime", endTime);
        map3.put("status", null);
        Integer orderCount = orderMapper.ordersStatistics(map3);
        Double orderCompletionRate = 0.0;
        if (orderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / orderCount.doubleValue();
        }
        Double unitPrice = 0.0;
        if (validOrderCount != 0) {
            unitPrice =turnover / validOrderCount;
        }
        return new BusinessDataVO(turnover, validOrderCount, orderCompletionRate,unitPrice , newUsers);
    }
}
